/*
 * Copyright 2007-2009 Thomas Krause (thomas@berlin-reloaded.de)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gidoo.cms.extra.tags;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jcr.RepositoryException;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.gidoo.cms.login.GidooCMSSession;

/**
 *
 * @author <a href="mailto:thomas@berlin-reloaded.de">Thomas Krause</a>
 */
public class TagCloud extends Panel
{
  public TagCloud(String id, GidooCMSSession session,
    final Class<? extends WebPage> tagListingPage)
  {
    super(id);
    
    Set<String> tagsRaw = Helper.getAllsTagsOfSite((GidooCMSSession) getSession());
    final Map<String, Integer> tags = new HashMap<String, Integer>();

    try
    {
      QueryManager qm = ((GidooCMSSession) getSession()).getQueryManager();

      for(String s : tagsRaw)
      {
        // find count of occurences of this tag and add to map
        QueryResult r = qm.createQuery("/jcr:root/sections/*//cms:md/tags/" + s, Query.XPATH).execute();

        tags.put(s, new Integer((int) r.getNodes().getSize()));
      }
    }
    catch(RepositoryException ex)
    {
      Logger.getLogger(TagCloud.class.getName()).log(Level.SEVERE, 
        "could not create tagcloud", ex);
    }

    final ArrayList<String> tagNames = new ArrayList<String>(tags.keySet());
    
    Collections.sort(tagNames);

    int max = 0;
    int min = Integer.MAX_VALUE;
    
    for(String s : tagNames)
    {
      max = Math.max(max, tags.get(s).intValue());
      min = Math.min(min, tags.get(s).intValue());
    }


    
    final int maxFinal = max;
    final int minFinal = min;
    
    ListView lsvTags = new ListView("lsvTags", tagNames) 
    {

      @Override
      protected void populateItem(ListItem item)
      {
        final String currentTagName = tagNames.get(item.getIndex());
        Link lnkTag = new Link("lnkTag") 
        {

          @Override
          public void onClick()
          {
            PageParameters pm = new PageParameters();
            pm.put("tag", currentTagName);
            setResponsePage(tagListingPage, pm);
          }
        };
        
        Label lblTag = new Label("lblTag", new Model(currentTagName));
        
        double a = (double) (tags.get(currentTagName).intValue() - minFinal);
        double b = (double) (maxFinal - minFinal);
        double p = b == 0 ? 0.5 : a / b;      
        
        
        lblTag.add(new AttributeModifier("style", true, 
          new Model("font-size:" 
          + (8.0 + p * (20.0 - 8.0))
          +"pt")));
        lnkTag.add(lblTag);
        item.add(lnkTag);
      }
    };
    lsvTags.setOutputMarkupId(true);

    add(lsvTags);
  }


}
